<template>
	<view class="coupon_pop">
		<u-popup :show="couponFlag" mode="bottom" closeable :round="10" @close="submit()"
			:overlayStyle="{'touch-action':'none'}">
			<view class="container" v-if="couponList.length > 0">
				<view class="conpon_container">
					<view v-for="(item,index) in couponList" @click="copClick(item,index)" :key="index" class="list">
						<!-- :class="currentList.includes(index)  ?'active':''" -->
						<view class="item_check">
							<!-- 遮罩层 -->
							<view class="mask"></view>
							<u-checkbox-group v-model="currentList" size="20">
								<u-checkbox shape="circle" :name="index" iconSize="20"></u-checkbox>
							</u-checkbox-group>
						</view>
						<view class="top2">
							<view style="display: flex;align-items: center;">
								<image style="width: 110rpx;padding-right: 24rpx;" :src="item.coupon.backImg"
									mode="widthFix">
								</image>
								<view>
									<view style="font-weight: 700;font-size: 32rpx;">{{item.coupon.name}}</view>
									<view style="font-size: 28rpx;color: #CA614E;margin-top: 12rpx;">
										{{ item.expireTime.split(/\s+/)[0] }}到期
									</view>
								</view>
							</view>
							<view style="display: flex;align-items: center;">
								<view style="font-size: 26rpx;color: #CA614E;">
									<text v-if="item.coupon.amount">￥</text>
									<text v-if="item.coupon.amount"
										style="font-size: 48rpx">{{item.coupon.amount}}</text>
								</view>
								<view></view>
							</view>
						</view>
					</view>
					<view style="width: 100vw; padding: 20rpx 0;box-sizing:border-box;z-index: 99;">
						<u-button @click="submit()">确定</u-button>
					</view>
				</view>

				<u-toast ref="uToast"></u-toast>

			</view>

			<view class="no_data" v-else>
				<view>
					您没有可用的优惠券
				</view>
				<view
					style="position: absolute;bottom: 0; width: 100vw; padding: 20rpx 0;box-sizing:border-box;z-index: 99;">
					<u-button @click="close()">关闭</u-button>
				</view>
			</view>
		</u-popup>
	</view>

</template>

<script>
	import {
		Find,
	} from '../../../api/balance.js'
	export default {
		props: {
			couponFlag: {
				type: Boolean,
				default: false
			},
			couponList: {
				type: Array,
				default: () => []
			},
			totalPrice: {
				type: Number,
				default: 0
			},
			checkedIds: {
				type: Array,
				default: () => []
			},
			reload: {
				type: Boolean,
				default: false
			},
			couponTypeNumber: {
				type: Number,
				default: -1
			}
		},
		data() {
			return {
				checkboxValue1: [],
				pageIndex: 1,
				pageSize: 999,
				currentindex: null, // 选中项优惠券index
				currentList: [],
				items: false,
				isWg: true, //如果没有外观检查服务 免费券自动disable
				orderId: '', // 订单ID
				couponsIds: [], // 优惠券ID,
				isiItems: false,
				formData: {},
				freeIds: [], // 免费券项目
				discounts: 0, //优惠金额
				couponType: -1
			};
		},
		watch: {
			reload(newValue, oldValue) {
				if (newValue) {
					this.currentList = []
					this.discounts = 0
					this.freeIds = []
				}
			},
			couponTypeNumber(newValue, oldValue) {
				if (newValue) {
					this.couponType = newValue
				}
			}
		},
		methods: {

			//arr1是否包含arr2
			getInclude(arr1, arr2) {
				let temp = []
				for (const item of arr2) {
					arr1.find(i => i === item) ? temp.push(item) : ''
				}
				return temp
			},

			// 点击 	// 0满减  1 现金  2免费
			copClick(e, index) {
				console.log("e", e);
				if (e.coupon.couponType == 0) {
					if (e.coupon.useAmount > this.totalPrice) {
						return this.$refs.uToast.show({
							message: `消费金额需达到${e.coupon.useAmount}才可使用此券`,
						})
					} else {
						this.discounts = e.coupon.amount
						this.couponType = e.coupon.couponType
					}
				}
				if (e.coupon.couponType == 1) {
					if (e.coupon.amount > this.totalPrice) {
						return this.$refs.uToast.show({
							message: `消费金额最低需达到${e.coupon.amount}才可使用此券`,
						})
					}
					this.discounts = e.coupon.amount
					this.freeIds = []
					this.couponType = e.coupon.couponType
				}
				if (e.coupon.couponType == 2) {
					let freeItemIds = e.coupon.freeItems.map(item => {
						return item.commodityId
					})
					this.freeIds = this.getInclude(this.checkedIds, freeItemIds)
					if (!this.freeIds.length) {
						this.freeIds = []
						return this.$refs.uToast.show({
							message: `此券只能用于${e.coupon.freeItems[0].commodityName}`,
						})
					}
					this.discounts = 0
					this.couponType = e.coupon.couponType
				}
				if (this.currentList.includes(index)) {
					this.currentList = []
					this.couponsIds = []
					this.discounts = 0
					this.freeIds = []
					this.couponType = -1
				} else {
					this.currentList = [index]
					this.couponsIds = [e.id]
				}
				this.isiItems = false
			},

			// 确认优惠券选择
			submit() {
				this.$emit('changeCoupon', {
					freeIds: this.freeIds,
					discounts: this.discounts,
					reload: false,
					couponType: this.couponType,
					couponsIds: this.couponsIds,
				});
			},

			//关闭
			close() {
				this.$emit('changeCoupon', {
					freeIds: [],
					discounts: 0,
					reload: true,
					couponType: -1,
					couponsIds: [],
				});
			}
		},

	}
</script>

<style lang="scss" scoped>
	.coupon_pop {
		width: 100%;
		height: 800rpx;
		background-color: #fff;
	}

	.container {
		width: 100%;
		height: 800rpx;
		background-color: #f5f5f5;
		padding: 80rpx 0 40rpx 0;
		box-sizing: border-box;
		overflow-y: scroll;
		overscroll-behavior: none; // 禁止滚动溢出

		.conpon_container {
			width: 100%;
			height: 100%;
			overflow-y: scroll;
		}
	}

	.no_data {
		width: 100%;
		height: 800rpx;
		background-color: #f5f5f5;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
		position: relative;
	}

	.list {
		height: 200rpx;
		margin: 20rpx 20rpx;
		padding: 20rpx 22rpx 0;
		box-sizing: border-box;
		border-radius: 8rpx;
		background-color: #fff;
		display: flex;
		align-items: center;

		.top2 {
			flex: 1;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-bottom: 10rpx;
			box-sizing: border-box;
			border-bottom: 1px dashed #ededed;
		}

		.bottom2 {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 35%;
		}

		.item_check {
			position: relative;

			.mask {
				position: absolute;
				width: 100%;
				height: 100%;
				z-index: 9;
				background-color: #fff;
				opacity: 0;
			}
		}
	}

	.active {
		background-color: #4058bb !important;
		color: #fff;
	}

	/deep/ .u-button--info {
		color: #fff !important;
		background-color: #4058bb !important;
		border-color: #4058bb !important;
	}

	/deep/ .u-button--square {
		border-bottom-left-radius: 50rpx !important;
		border-bottom-right-radius: 50rpx !important;
		border-top-left-radius: 50rpx !important;
		border-top-right-radius: 50rpx !important;
	}

	/deep/ .u-button {
		width: 85vw !important;
	}
</style>